;var jfPDAvoidMinifyErrors=1;(function($, document, window) {

  var ns = jfPD,
      $document = $(document),
      isPhonegapApp = window.cordova !== undefined || window.PhoneGap !== undefined,
      isFirstPage = true;

  ns.appType = 'native';
      
  function onDeviceReady() {
    if (isPhonegapApp) {
      var navigator = window.navigator;

      ns.controller.isOffline = function() {
        return navigator.network.connection.type === Connection.NONE;
      };
      
      document.addEventListener("online", ns.controller.showNetworkStateMessage);

      document.addEventListener('backbutton', function() {
        if ($.mobile.urlHistory.activeIndex <= 1) { //the first entry (index 0) is a blank page, the second entry (index 1) is the first 'real' page
          if (navigator.app) {
            navigator.app.exitApp();
          }
        }
        else {
          window.history.back();
        }
      }, false);

      ns.controller.showNetworkStateMessage();
    }
  }
  
  function onPageBeforeCreate(e) {
    if (isFirstPage) { //first page is a blank page that should be ignored
      isFirstPage = false;

      return;
    }

    $(e.currentTarget).find('a[data-childbrowser]')
       .attr('data-ajax', 'false')
       .bind(ns.controller.clickEvent, onClickOpenChildbrowser);
  }
  
  function onPageShow(e) {
    if (isFirstPage) { //first page is a blank page that should be ignored
      isFirstPage = false;

      return;
    }

    hideBackButtonIfFirstPage($(this));
    hideSplashScreen();
  }

  var splashScreenIsVisible = true;
  function hideSplashScreen() {
    if (!splashScreenIsVisible || !isPhonegapApp) {
      return;
    }

    try {
      cordova.exec(null, null, "SplashScreen", "hide", []);
      splashScreenIsVisible = false;
    }
    catch(e) {
      setTimeout(hideSplashScreen, 2500);
    }
  }
  
  function hideBackButtonIfFirstPage($page) {
    var $button = $page.find('[data-rel=back]');
    if ($.mobile.urlHistory.activeIndex <= 1) {
      $button.hide();
    }
    else {
      $button.show();
    }
  }

  function onClickOpenChildbrowser(e) {
    var $link = $(this),
      url = $link.attr('href'),
      isSameDomain = ns.controller.isSameDomain(url);

    e.stopPropagation();
    e.preventDefault();
    
    if (isSameDomain && $link.attr('data-childbrowser') !== 'true') {
      ns.controller.loadPage(url);
      //only enforce childbrowser for same fomain if data-childbrowser attribute is explicitly set to 'true'
      return;
    }

    if (!url.match(/^http/)) {
      return;
    }

    window.open(url, '_blank', 'location=yes');

    return false;
  }
  
  function onPageInit(e) {
    if (isFirstPage) { //first page is a blank page that should be ignored
      isFirstPage = false;

      return;
    }

    var $page = $(this);
    setTimeout(forceTargetBlankForExternalLinks.bind(null, $page), 0);
  }
  
  function forceTargetBlankForExternalLinks($page) {
    $page.find('a[href^=http]').each(function(idx, el) {
      if (!ns.controller.urlIsHijackable(el.href)) {
        $(el).click(function(e) {
          if (e.isImmediatePropagationStopped()) {
            return;
          }

          var childBrowser = $(this).attr('data-childbrowser');
          if (childBrowser !== undefined && childBrowser !== 'false') {
            window.open(this.href, '_blank', 'location=yes');
            e.preventDefault();
            e.stopImmediatePropagation();
          }
          else if (this.target !=='_blank') { //target=_blank is already handled by forceSystemBrowserForTargetBlankLinks()
            window.open(this.href, '_system');
            e.preventDefault();
            e.stopImmediatePropagation();
          }
        });
      }
    });
  }
  
  function forceSystemBrowserForTargetBlankLinks() {
    $document.delegate('a[target="_blank"]', 'click', function(e) {
      if (!e.isImmediatePropagationStopped() && this.href) {
        e.preventDefault();
        window.open(this.href, '_system');
      }
    });
  }
  
  $.support.cors = true;
  $.ajaxSetup({
    headers: {'X-Requested-With': 'XMLHttpRequest' } //sometimes X-REQUESTED-WITH header is missing and thus some frameworks (e.g. Symfony) won't recognized that the request is an XHR request
  }); 
  $.mobile.allowCrossDomainPages = true;
  $.mobile.pushStateEnabled = false; //ATTENTION: Disable pushState if you encounter problems on android version < 4!
  
  document.addEventListener("deviceready", onDeviceReady, false); //event must not be bound using jQuery but using document.addEventListener!
  
  forceSystemBrowserForTargetBlankLinks();
  
  $document.
    delegate("div:jqmData(role='page')", 'pageshow', onPageShow).
    delegate("div:jqmData(role='page')", 'pageinit', onPageInit).
    delegate("div:jqmData(role='page')", 'pagebeforecreate', onPageBeforeCreate).
    bind('jfPD:init', function() { 
      ns.controller.loadPage(ns.config.startUrl);
    });

})(jQuery, document, window);